package com.we823.cms.controller.manage.cms;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.controller.manage.AbstractManageController;
import com.we823.cms.model.cms.News;
import com.we823.cms.repository.service.cms.NewsChannelService;
import com.we823.cms.repository.service.cms.NewsService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL+"/news")
public class NewsManageController extends AbstractManageController<News, Integer> {

	private String baseUrl = WebConstants.WEB_MANAGE_TEMPLATE_BaseUrl + "news/";
	
	private NewsService newsService;
	private @Resource NewsChannelService newsChannelService;
	
	@Autowired
	public NewsManageController(NewsService newsService) {
		super(newsService);
		this.newsService = newsService;
		super.baseUrl = baseUrl;
		super.entityName="news";
	}

	@Override
	public PageInfo<News> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<News> newsPage;
		int newsChannelId = RequestUtil.getValueByName(request, "newsChannelId", -1);
		if(newsChannelId==-1) {
			if(pageable){
				newsPage = newsService.findAll(pageRequest);
			}else{
				newsPage = new PageInfo<>(newsService.findAll());
			}
		}else {
			if (pageable) {
				newsPage=newsService.findByNewsChannel(newsChannelId, pageRequest);
			}else {
				newsPage = new PageInfo<>(newsService.findByChannel(newsChannelId, getSort(request)));
			}
		}
		return newsPage;
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("cms:news_channel");
	}

}
